import cv2

if __name__ == "__main__":
    path = "work2.jpg"
    image_np = cv2.imread(path)
    image_np_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)  # 灰度化


    maxval = 255
    # image_np_adaptive = cv2.adaptiveThreshold(image_np_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 7, 10)
    ret, image_np_thresh = cv2.threshold(image_np_gray, 127, maxval, cv2.THRESH_BINARY)
    # cv2.imshow('finall_image', image_np_thresh)

    kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
    kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (23, 23))
    # kernel3 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))

    morph_image1 = cv2.erode(image_np_thresh, kernel1)
    # cv2.imshow('fist_image', morph_image1)
    morph_image2 = cv2.dilate(morph_image1, kernel2)
    # cv2.imshow('second_image', morph_image2)

    # morph_image3 = cv2.erode(morph_image2, kernel3)

    cv2.imshow('finall_image', morph_image2)
    cv2.waitKey(0)
    cv2.imwrite('zuoye2.jpg', morph_image2)
